﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using lumo.display.object2D;

namespace lumo.display.animation2D
{
    /// <summary>
    /// A position animation
    /// </summary>
    public class PositionAnimation2D : GenericAnimation2D<IPositionable2D>
    {
        // Private variables
        private Vector2? From = null;
        private Vector2 To;
        private Counter XCounter = new Counter();
        private Counter YCounter = new Counter();

        /// <summary>
        /// Constructor
        /// </summary>
        public PositionAnimation2D(Vector2 To, float Time, Smooth Smoothness = Smooth.None, Loop LoopMode = Loop.None, IPositionable2D CurrentObject = null)
            : base(Time, Smoothness, LoopMode, CurrentObject)
        {
            this.To = To;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public PositionAnimation2D(Vector2? From, Vector2 To, float Time, Smooth Smoothness = Smooth.None, Loop LoopMode = Loop.None, IPositionable2D CurrentObject = null)
            : base(Time, Smoothness, LoopMode, CurrentObject)
        {
            this.From = From;
            this.To = To;
        }

        /// <summary>
        /// Check if finished
        /// </summary>
        public override bool Finished { get { return (XCounter.Finished && YCounter.Finished); } }

        /// <summary>
        /// Setup the values
        /// </summary>
        internal override void SetupValues()
        {
            Vector2? From = this.From;
            if (!From.HasValue)
                From = this.CurrentObject.Position;
            XCounter.Reset(From.Value.X, this.To.X, this.Time, this.Smoothness, this.LoopMode);
            YCounter.Reset(From.Value.Y, this.To.Y, this.Time, this.Smoothness, this.LoopMode);
        }

        /// <summary>
        /// Update the values.
        /// </summary>
        /// <param name="Game">The game class</param>
        internal override void UpdateValues(LumoComponent Game)
        {
            XCounter.Update(Game);
            YCounter.Update(Game);
            this.CurrentObject.Position = new Vector2(XCounter.Value, YCounter.Value);
        }
    }
}
